package top.zhangjianyong.blog.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import top.zhangjianyong.blog.entity.dto.FileDTO;
import top.zhangjianyong.blog.entity.po.WeiboFile;
import top.zhangjianyong.blog.mapper.WeiboFileMapper;
import top.zhangjianyong.blog.service.WeiboFileService;

/**
 * @author zhangjianyong
 * @since 2025/5/6 15:55
 */
@Service
public class WeiboFileServiceImpl extends ServiceImpl<WeiboFileMapper, WeiboFile>
    implements WeiboFileService {

  private final WeiboFileService weiboFileService;

  public WeiboFileServiceImpl(@Lazy WeiboFileService weiboFileService) {
    this.weiboFileService = weiboFileService;
  }

  @Override
  public List<FileDTO> listFileByWeiboId(Long weiboId) {
    return baseMapper.listFileByWeiboId(weiboId);
  }

  @Override
  public void updateFiles(Long weiboId, List<Long> files) {

    // 删除
    lambdaUpdate().eq(WeiboFile::getWeiboId, weiboId).remove();
    // 添加
    if (CollectionUtil.isNotEmpty(files)) {
      List<WeiboFile> list =
          files.stream()
              .map(
                  fileId -> {
                    WeiboFile weiboFile = new WeiboFile();
                    weiboFile.setFileId(fileId);
                    weiboFile.setWeiboId(weiboId);
                    return weiboFile;
                  })
              .toList();
      weiboFileService.saveBatch(list);
    }
  }
}
